package com.AAL.DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.ITypesOfWheelDAO;
import com.AAL.Model.TypeOfAuto;
import com.AAL.Model.TypesOfWheel;
import com.AAL.Utils.HibernateUtil;

public class TypesOfWheelDAO implements ITypesOfWheelDAO {

	@Override
	public TypesOfWheel saveAndUpdateTypesOfWheel(TypesOfWheel type) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(type);
		} catch (Exception e) {
			System.out.println("Eror:"+ e.getMessage());
			return null;
		}
		session.getTransaction().commit();
		return type;
	}

	@Override
	public TypesOfWheel deleteTypesOfWheel(String typeID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			TypesOfWheel typesOfWheel = (TypesOfWheel) session.load(
					TypesOfWheel.class, typeID);
			if (null != typesOfWheel) {
				session.delete(typesOfWheel);
			}
			session.getTransaction().commit();
			return typesOfWheel;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}

	}

	@Override
	public TypesOfWheel getTypesOfWheel(String typeID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("getTypesOfWheel");
		TypesOfWheel typesOfWheel = (TypesOfWheel) session.load(
				TypesOfWheel.class, typeID);
		return typesOfWheel;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TypesOfWheel> getAllTypesOfWheels() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<TypesOfWheel> lstTypesOfWheels = null;
		try {
			lstTypesOfWheels = (List<TypesOfWheel>) session.createQuery(
					"from TypesOfWheel").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstTypesOfWheels;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TypesOfWheel> getLstTypesOfWheelsByTypeOfAutos(
			TypeOfAuto typeOfAutos) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<TypesOfWheel> lstTypesOfWheels = null;
		try {
			lstTypesOfWheels = (List<TypesOfWheel>) session
					.createQuery(
							"from TypesOfWheel where typeOfAuto.typeOfAutoID=:typeOfAutoID")
					.setParameter("typeOfAutoID", typeOfAutos.getTypeOfAutoID())
					.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstTypesOfWheels;
	}

}
